<?php
namespace  Admin\Controller;
use Think\Controller;
/*header('Content-type:text/html;charset:utf-8');*/
class AddverController extends BaseController{
    public function index(){
        $model=M('addver');
        $condition['name'] = array('like',"%".$_GET['name']."%");
        if(!empty($_GET)){
            unset($_SESSION['like']);
            $_SESSION['like']=$_GET['name'];
        }
        $count = $model->where( $condition)->count();
        $page= new \Think\Page($count,5);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        $show = $page->show();
        $list=$model->where($condition)->order('created_at desc')->limit($page->firstRow.','.$page->listRows)->select();
        foreach($list as $key => $val){
            $list[$key]['picture']=$this->substr($val['picture']).$val['picture'];
            $list[$key]['status']=$val['status']==10?'可用':'禁用';
        }
        $this->assign('name',$_SESSION['like']);
        $this->assign('page',$show);
        $this->assign('data',$list);
        $this->display();
    }
    public function create(){
        if(empty($_POST)){
            $this->display();
            exit;
        }
        $data['name'] = $_POST['name'];
        if(empty($data['name'])) {
            $this->error('广告名不能为空！');
        }
        $pattern='/
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/';
        if(empty($_POST['url'])){
            $this->error('链接不能为空');
        }
        if(!preg_match($pattern,$_POST['url'])){
            $this->error('链接格式错误');
        }
        $data['picture']=$this->upload();
        $data['status']=$_POST['status'];
        $data['url']=$_POST['url'];
        $data['created_at']=time();
        $res=M('addver')->add($data);
        if($res){
            $this->success('广告插入成功','index');
        }else{
            $this->error('广告插入失败');
        }

    }
    public function updated(){
        if($_GET['key']==11){
            if(empty($_GET['id'])){
                $this->error('没有id');
            }
            $id=$_GET['id'];
            $list=M('addver')->where('id='.$id)->find();
            $data['status']=$list['status']==10?30:10;
            $res=M('addver')->where('id='.$id)->save($data);
            if($res){
                $this->redirect('index');
            }else{
                $this->error('修改失败');
            }
            exit;
        }
        if($_GET['key']==22){
            if(empty($_POST['name'])){
                $this->error('广告名不能为空');
                exit;
            }
            if(empty($_POST['url'])){
                $this->error('链接不能为空');
                exit;
            }
            if($_FILES['pic']['type']!==''){
                $data['picture']=$this->upload();
            }
            $data['name']=$_POST['name'];
            $data['status']=$_POST['status'];
            $data['url']=$_POST['url'];
            if($_SESSION['temp']['status']===$data['status']){
                unset($data['status']);
            }
            if($_SESSION['temp']['url']===$data['url']){
                unset($data['url']);
            }
            unset($_SESSION['temp']);
            $data['updated_at']=time();
            $res=M('addver')->where('id='.$_GET['id'])->save($data);
            if($res){
                $this->redirect('index');
            }else{
                $this->error('广告修改失败');
            }
        }
        if(empty($_GET)){
            $this->error('没有id');
        }
        $id=$_GET['id'];
        $list=M('addver')->where('id='.$id)->find();
        $_SESSION['temp']=$list;
        $list['picture']=$this->substr($list['picture']).$list['picture'];
        $this->assign('data',$list);
        $this->display();

    }
    public function deleted(){
        $id=$_GET['id'];
        $res=M('addver')->where('id='.$id)->delete();
        if($res){
            $this->redirect('index');
        }else{
            $this->error('删除失败');
        }
    }

}